package mac10.monitor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import mac10.models.Cell;
import mac10.models.WorldModel;

public class Monitor {

	private static JFrame frame = new JFrame("Monitor");
	private static JPanel container;
	private static JScrollPane scrollPane;
	private static JMenuBar menuBar;
	private static ImageComponent ImgComp[][];

	private static BufferedImage agent_blue, agent_red, agent_red_dark, cow,
			cow_dark, fence, fence_dark, grass, grass_dark, stable1,
			stable1_dark, stable2, stable2_dark, lever, lever_dark, tree,
			tree_dark, blank;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void initializeMonitor(WorldModel worldModel) {
		frame.getContentPane().removeAll();
		container = new JPanel();
		scrollPane = new JScrollPane();
		menuBar = new JMenuBar();
		Monitor.readImages();
		int countx = worldModel.getWidth();
		int county = worldModel.getHeight();

		int gridCellHeight = 14; // Math.min((int) 800/countmax,32);
		int gridCellWidth = 14; // Math.min((int) 800/countmax,32);

		Monitor.container.setLayout(null);
		ImgComp = new ImageComponent[county][countx];
		for (int x = 1; x <= countx; x++) {
			for (int y = 1; y <= county; y++) {
				Monitor.ImgComp[x - 1][y - 1] = new ImageComponent(
						gridCellWidth, gridCellHeight);
				Monitor.ImgComp[x - 1][y - 1].setBounds(x * gridCellWidth, y
						* gridCellHeight, gridCellWidth, gridCellHeight);
				Monitor.ImgComp[x - 1][y - 1].setImage(Monitor.grass);
				Monitor.container.add(Monitor.ImgComp[x - 1][y - 1]);
			}
		}

		Dimension frameSize = new Dimension(Math.min(gridCellWidth
				* (countx + 2), 800), Math.min(gridCellHeight * (county + 2)
				+ 32, 800));

		frame.setSize(frameSize);
		scrollPane.getVerticalScrollBar().setUnitIncrement(15);
		scrollPane.getHorizontalScrollBar().setUnitIncrement(15);
		scrollPane.setSize(new Dimension((int) frameSize.getWidth() - 5,
				(int) frameSize.getHeight() - 27));
		container.setPreferredSize(new Dimension(gridCellWidth * (countx + 2),
				gridCellHeight * (county + 2)));
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int p = (d.width - frame.getSize().width) / 2;
		int q = (d.height - frame.getSize().height) / 2;
		frame.setLocation(p, q);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		container.setBackground(Color.black);
		frame.setTitle("Monitor");
		frame.getContentPane().setLayout(null);
		scrollPane.setViewportView(container);
		frame.getContentPane().add(scrollPane);

		JMenu menu = new JMenu("System");
		menu.setMnemonic('S');
		menu.add(new JMenuItem());
		menuBar.add(menu);
		// frame.setJMenuBar( menuBar );

		frame.setVisible(true);
	}

	private static void readImages() {
		try {
			Monitor.agent_blue = ImageIO.read(new File(
					"src/main/resources/img/agent_blue.png"));
			Monitor.agent_red = ImageIO.read(new File(
					"src/main/resources/img/agent_red.png"));
			Monitor.agent_red_dark = ImageIO.read(new File(
					"src/main/resources/img/agent_red_dark.png"));
			Monitor.cow = ImageIO.read(new File(
					"src/main/resources/img/cow.png"));
			Monitor.cow_dark = ImageIO.read(new File(
					"src/main/resources/img/cow_dark.png"));
			Monitor.fence = ImageIO.read(new File(
					"src/main/resources/img/fence.png"));
			Monitor.fence_dark = ImageIO.read(new File(
					"src/main/resources/img/fence_dark.png"));
			Monitor.grass = ImageIO.read(new File(
					"src/main/resources/img/grass.png"));
			Monitor.grass_dark = ImageIO.read(new File(
					"src/main/resources/img/grass_dark.png"));
			Monitor.stable1 = ImageIO.read(new File(
					"src/main/resources/img/Stable1.png"));
			Monitor.stable1_dark = ImageIO.read(new File(
					"src/main/resources/img/Stable1_dark.png"));
			Monitor.stable2 = ImageIO.read(new File(
					"src/main/resources/img/Stable2.png"));
			Monitor.stable2_dark = ImageIO.read(new File(
					"src/main/resources/img/Stable2_dark.png"));
			Monitor.lever = ImageIO.read(new File(
					"src/main/resources/img/switch.png"));
			Monitor.lever_dark = ImageIO.read(new File(
					"src/main/resources/img/switch_dark.png"));
			Monitor.tree = ImageIO.read(new File(
					"src/main/resources/img/tree.png"));
			Monitor.tree_dark = ImageIO.read(new File(
					"src/main/resources/img/tree_dark.png"));
			Monitor.blank = ImageIO.read(new File(
					"src/main/resources/img/blank.png"));
		} catch (IOException e) {
		}

	}

	public static synchronized void drawWorldModel(WorldModel worldModel,
			int timestamp) {
		int countx = worldModel.getWidth();
		int county = worldModel.getHeight();

		BufferedImage image;
		for (int x = 0; x < countx; x++) {
			for (int y = 0; y < county; y++) {
				Cell cell = worldModel.getCell(x, y);
				if (cell.isUnknown())
					image = blank;
				else if (cell.isCow())
					if (cell.getTimestamp() == timestamp)
						image = cow;
					else
						image = cow_dark;
				else if (cell.isAllyAgent())
					image = agent_blue;
				else if (cell.isEnemyAgent())
					if (cell.getTimestamp() == timestamp)
						image = agent_red;
					else
						image = agent_red_dark;
				else if (cell.isObstacle())
					if (cell.getTimestamp() == timestamp)
						image = tree;
					else
						image = tree_dark;
				else if (cell.isAllyCorral())
					if (cell.getTimestamp() == timestamp)
						image = stable1;
					else
						image = stable1_dark;
				else if (cell.isEnemyCorral())
					if (cell.getTimestamp() == timestamp)
						image = stable2;
					else
						image = stable2_dark;
				else if (cell.isFence() && !cell.isOpen())
					if (cell.getTimestamp() == timestamp)
						image = fence;
					else
						image = fence_dark;
				else if (cell.isSwitch())
					if (cell.getTimestamp() == timestamp)
						image = lever;
					else
						image = lever_dark;
				else if (cell.getTimestamp() == timestamp)
					image = grass;
				else
					image = grass_dark;

				Monitor.ImgComp[x][y].setImage(image);
			}
		}

		container.repaint();
	}

//	 public static void main(String args[]) throws InterruptedException {
//	 Monitor.initializeMonitor(new WorldModel(80,80, 0, 0, 0, 0));
//	 }
}
